"""Logger管理类"""
import logging
from typing import Optional
from config_manager import ConfigManager


class LoggerManager:
    _instance: Optional['LoggerManager'] = None
    _logger: Optional[logging.Logger] = None

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
            cls._setup_logger()
        return cls._instance

    @classmethod
    def _setup_logger(cls) -> None:
        """设置日志配置"""
        config = ConfigManager()
        log_level = config.get_config('log_level', 'INFO').upper()
        # 将字符串日志级别转换为logging模块的级别
        level_map = {
            'DEBUG': logging.DEBUG,
            'INFO': logging.INFO,
            'WARNING': logging.WARNING,
            'ERROR': logging.ERROR,
            'CRITICAL': logging.CRITICAL
        }
        level = level_map.get(log_level, logging.INFO)  # 默认使用 INFO

        logging.basicConfig(
            level=level,
            format='%(asctime)s [%(levelname)s] %(message)s',
            datefmt='%Y-%m-%d %H:%M:%S'
        )
        cls._logger = logging.getLogger(__name__)

    @classmethod
    def get_logger(cls) -> logging.Logger:
        """获取logger实例"""
        if cls._logger is None:
            cls._setup_logger()
        return cls._logger
